/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	/**
	 *  DataRow 创建一个数据行
	 * @author Administrator
	 * 
	 */	
	public class EDIDataRow
	{
		private var _field:Array; 
		private var _tempField:Array;
		private var _fieldObj:Object;
		private var _noField:Boolean=false;
		private var _table:EDIDataTable;
		
		/**
		 * 构造一个新行 
		 * @param field 该行中的字段名称
		 * 
		 */				
		public function EDIDataRow(field:Array,table:EDIDataTable)
		{
			_field=field;
			_fieldObj=new Object();
			_table = table;
			for(var i:int=0;i<field.length;i++)
			{
				_fieldObj[field[i]["field"]]=field[i]["value"];
			}
		}
		
		/**
		 * 设置某字段的值 
		 * @param fieldName 字段名
		 * @param fieldValue 值
		 * @return 
		 * 
		 */		
		public function setCell(fieldName:String,fieldValue:String):Boolean
		{
			var ret:Boolean=false;
			fieldName=fieldName.toLowerCase();
			if(_field.length==0)
			{
				_noField=true;
			}
			
			if(_fieldObj.hasOwnProperty(fieldName))
			{
				_fieldObj[fieldName]=fieldValue;
				
				for(var i:int=0;i<_field.length;i++)
				{
					var obj:Object=_field[i];
					if(obj["field"]==fieldName)
					{
						obj["value"]=fieldValue;
						break;
					}
				}
				
				ret=true;
			}
//			else if(_noField)
//			{
//				addField(fieldName,fieldValue)	
//				ret=true;	
//			}
			return ret;
		}
		
		/**
		 * 添加新字段 
		 * @param filedName 字段名
		 * @param fieldValue 字段值
		 * @param fieldType	字段类型
		 * @param primaryKey	主键
		 * 
		 */		
		private function addField(filedName:String,fieldValue:String,fieldType:String="",primaryKey:String=""):void
		{
			var obj:Object=new Object();
			obj=new Object();
			obj["field"]=filedName
			obj["fieldType"]=fieldType;
			obj["primaryKey"]=primaryKey;
			obj["value"]=fieldValue;
			_field.push(obj);
			_fieldObj[filedName]=fieldValue;
		}
		
		/**
		 * 生成该行的xml信息 
		 * @return 
		 * 
		 */		
		public function toRow():XML
		{
			var retValue:XML=new XML(<row />);
			for(var i:int=0;i<_field.length;i++)
			{
				retValue.appendChild("<"+_field[i]["field"]+"><![CDATA["+_fieldObj[_field[i]["field"]]+"]]></"+_field[i]["field"]+">");
			}
			return retValue;
		}
		
		/**
		 * 获得该行的所有字段信息。 
		 * @return  
		 * 
		 */		
		public function get rowFiled():Array
		{
			return _field;
		}
		
		/**
		 * 很据字段名，获得该字段的值 
		 * @param fieldName 字段名
		 * @return 
		 * 
		 */		
		public function getFieldValue(fieldName:String):String
		{
			return _fieldObj[fieldName.toLowerCase()].toString();
		}
	}
}